package 剑指Offer.简单;

import java.util.Stack;

/**
 * @author caihe
 * @date 2022/8/23 23:21
 */
public class _30包含min函数的栈 {


    Stack<Integer> A,B;
    /** initialize your data structure here. */
    public _30包含min函数的栈() {
        A = new Stack();
        B = new Stack();
    }

    public void push(int x) {
        A.add(x);
        if(B.empty() || B.peek() >= x) {
            // 将小于peek的元素添加到B中
            B.add(x);
        }
    }

    public void pop() {
        if(A.pop().equals( B.peek())) {
            B.pop();
        }
    }

    public int top() {
        return A.peek();
    }

    public int min() {
        return B.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */